#!/bin/sh
# shellcheck disable=SC2034
RUNNING_USER=root
ADATE=`date +%Y%m%d%H%M%S`

NAME=$2
METHOD=$3

deploy(){
  cd /var/app/server || exit
  if [ -f "$NAME-bin.zip" ]; then
    lsattr -a $NAME/*
    lsattr -i $NAME/*
    rm -rf $NAME/*
    unzip -o $NAME-bin.zip
    cd $NAME || exit
    echo "sh ./run-$NAME.sh deploy"
    sh ./run-$NAME.sh deploy
  fi
}

start(){
  cd /var/app/server/$NAME || exit
  if [ -f "run-$NAME.sh" ]; then
    echo "sh ./run-$NAME.sh start"
    sh ./run-$NAME.sh start
  fi
}

restart(){
  cd /var/app/server/$NAME || exit
  if [ -f "run-$NAME.sh" ]; then
    echo "sh ./run-$NAME.sh restart"
    sh ./run-$NAME.sh restart
  fi
}


unzip_restart(){
  cd /var/app/server/
  rm -rf $NAME/*
  unzip -o /var/app/server/$NAME-bin.zip
  cd /var/app/server/$NAME || exit
  if [ -f "run-$NAME.sh" ]; then
    echo "sh ./run-$NAME.sh restart"
#    lsattr -a $NAME/*
#    lsattr -i $NAME/*
    sh ./run-$NAME.sh restart
  fi
}

stop(){
  cd /var/app/server/$NAME || exit
  if [ -f "run-$NAME.sh" ]; then
    echo "sh ./run-$NAME.sh stop"
    sh ./run-$NAME.sh stop
  fi
}

script(){
  cd /var/app/server/$NAME || exit
  echo "sh ./run-$NAME.sh $METHOD"
  sh ./run-$NAME.sh $METHOD
}

case $1 in
          deploy) deploy;;
          start) start;;
          stop) stop;;
          script)  script;;
          unzip_restart)  unzip_restart;;
          restart)  restart;;
              *)  echo "参数1=>执行方法,参数2=>app名称,参数3=>script方法\n方法: unzip_restart 解压运行|deploy 部署|start 启动|stop 停止|script 执行对应app run-*.sh "  ;;
esac